<?php

class Corector extends Controller {

	public function __construct() {
		parent::__construct();
		if (! $this->usersession->userLoggedIn()) {
			$aResponse = array("error" => TRUE, "description" => "Sesiunea a expirat", "type" => "nosession");
			die(json_encode($aResponse));
		}
		$this->load->model('model_corector');
		$this->aUser = $this->usersession->currentUser();
		copyPost($this->aPost);
	}

	//functia de aducere a obiectelor
	function getClientCampanie() {
		echo json_encode($this->model_corrector->getClientCampanie($this->aPost));
	}

	//functie de editare a numelui unui obiect sau subobiect
	function editObject() {
		echo json_encode($this->model_corrector->editObject($_POST));
	}

	function transfer() {
		echo json_encode($this->model_corrector->transfer($_POST));
	}

	//functie de stergere obiecte si subobiecte
	function delete() {
		echo json_encode($this->model_corrector->delete($_POST));
	}

	/**
	 * Functie de export
	 */
	function export() {

		$sTable = $this->uri->segment(3);
		$where['name'] = $this->uri->segment(4);

		$result = $this->model_corrector->bringObject($sTable, $s_limit, $e_limit, $sort, $dir, $where);


		header("Content-type: application/vnd.ms-excel;charset=utf-8");
		header("Expires: 0");
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
		header("Content-disposition:  attachment; filename=raport.xls");
		$this->load->library('xmlexcel');
		$newexcel = new Xmlexcel;

		$newexcel->write_header();
		$newexcel->write_document_properties('MTP');
		$newexcel->write_styles();

		$newexcel->start_worksheet();
		$newexcel->write_row('
		<Column ss:Width="400"/>
		<Column ss:Width="400"/>');


		$newexcel->write_row('
		<Row>		   
		 <Cell ss:StyleID="headerStyle"><Data ss:Type="String">OBIECT</Data></Cell>
		 <Cell ss:StyleID="headerStyle"><Data ss:Type="String">SUBOBIECT</Data></Cell>    	   
		</Row>');


		foreach ($result['DS_OBJECT'] as $key => $row) {

			if ($row["level"] == "0") {
				$sObj = $row["obj"];
				$sSubobj = "";
			} else {
				$sObj = "";
				$sSubobj = $row["obj"];
			}


			$newexcel->write_row('<Row>
		    <Cell ss:StyleID="defaultComp"><Data ss:Type="String">'.strTitle(str_replace($litere_er, $litere_sol, $sObj)).'</Data></Cell>
		    <Cell ss:StyleID="defaultComp"><Data ss:Type="String">'.strTitle(str_replace($litere_er, $litere_sol, $sSubobj)).'</Data></Cell>
		    </Row>');
		}

		$newexcel->end_worksheet();
		$newexcel->write_footer();
		print $newexcel->output();
		exit;

	}

}